﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using SIPO.Library.DataAccess.Report;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using SIPO.Web.Support;
using Microsoft.Reporting.WebForms;
using System.Threading.Tasks;
using System.Drawing;

namespace SIPO.Web.lists.Report
{
    public partial class rptPenerimaanlist : BasePage
    {
        recAcceptanceRepo Acceptance = new recAcceptanceRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        recAcceptanceDetailRepo AcceptanceDetail = new recAcceptanceDetailRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
       
        protected override void OnLoad(EventArgs e)
        {
           
            if (!IsPostBack)
            {
                
                List<recAcceptance> query = Acceptance.GetList();

                

                IEnumerable<recAcceptance> transTypes = query.OrderBy(x => x.AcceptancePeriod).GroupBy(car => car.AcceptancePeriod).Select(g => g.First()).ToList();
               
               
                ddlPeriode0.Items.Clear();
                ddlPeriode1.Items.Clear();
                if (transTypes != null)
                {
                    foreach(recAcceptance Accept in transTypes)
                    {
                       // ddlPeriode.Items.Add(new ListItem(Accept.AcceptancePeriod.ToString(), Accept.AcceptancePeriod.ToString()));
                        ddlPeriode0.Items.Add(new ListItem(Accept.AcceptancePeriod.ToString(), Accept.AcceptancePeriod.ToString()));
                        ddlPeriode1.Items.Add(new ListItem(Accept.AcceptancePeriod.ToString(), Accept.AcceptancePeriod.ToString()));
                        
                    }
               // ddlPeriode.SelectedIndex = 0;
                ddlPeriode0.SelectedIndex = 0;
                ddlPeriode1.SelectedIndex = 0;
                }

                IEnumerable<recAcceptance> ddl = query.FindAll(x => x.AcceptancePeriod.ToString() == ddlPeriode0.SelectedValue).OrderByDescending(x => x.Versi);
                if (ddl != null)
                {
                    foreach (recAcceptance Accept in ddl)
                    {
                        ddlVersi.Items.Add(new ListItem(Accept.Versi.ToString(), Accept.Versi.ToString()));                     
                    }
                }
              
            }
        }
        

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string parameter = string.Empty;

            parameter = "between " + ddlPeriode0.SelectedValue + " and " + ddlPeriode1.SelectedValue + " and transactions.recacceptancedetail.AcceptanceId in (select max(acceptanceid) from transactions.recAcceptance group by AcceptancePeriod)";
            string parameter2 = "between " + ddlPeriode0.SelectedValue + " and " + ddlPeriode1.SelectedValue;
                string period = "Tahun " + ddlPeriode0.SelectedValue + " s.d " + ddlPeriode1.SelectedValue;
                if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
                {
                    period = "Tahun " + ddlPeriode0.SelectedValue;
                }

                if (ddlPeriode0.SelectedValue == ddlPeriode1.SelectedValue)
                {
                    parameter = "= " + ddlPeriode0.SelectedValue +" and Versi=" + ddlVersi.SelectedValue;
                }

               
                try
                {
                   // DataTable dt = AcceptanceDetail.GetDataReport(parameter,parameter2);

                    rsViewer.Reset();
                    string path = "~/reportviewer/Transaction/rptTargetPenerimaan.rdlc";
                    rsViewer.LocalReport.ReportPath = Server.MapPath(path);
                    ReportDataSource rds = new ReportDataSource();
                    rds.Name = "DataSet1";
                   // rds.Value = dt;
                    ReportParameter p1 = new ReportParameter("Periode", period);
                    this.rsViewer.LocalReport.SetParameters(new ReportParameter[] { p1 });

                    rsViewer.LocalReport.DataSources.Clear();
                    rsViewer.LocalReport.DataSources.Add(rds);
                    rsViewer.LocalReport.Refresh();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            



            
          
            
            
               
            

        }


    }
}